import { useMessage } from '@hooks/useAppMessage'
import { useAxios } from '@hooks/useAxios'
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
import { API_NEWS_CATEGORY_URL, API_NEWS_URL } from './baseApi'

export const useNewsQuery = ({ type, title, page, per_page, sort }) => {
  const { http } = useAxios()
  return useQuery({
    queryKey: ['news.list', { type, title, page, per_page, sort }],
    queryFn: async () => {
      return await http.get(API_NEWS_URL, {
        params: {
          ...(type && { type }),
          ...(title && { title }),
          sort,
          page,
          per_page,
        },
      })
    },
  })
}

export const useNewsDetailQuery = (id) => {
  const { http } = useAxios()
  return useQuery({
    queryKey: ['news.detail', id],
    queryFn: async () => {
      const response = await http.get(`${API_NEWS_URL}/${id}`)
      return response.data
    },
    enabled: !!id,
  })
}

/**
 *
 * @param {Function} cb 成功后的回调
 * @returns
 */
export const useNewsUpdateMutation = () => {
  const { http } = useAxios()
  const queryClient = useQueryClient()
  const message = useMessage()
  return useMutation({
    mutationFn: async (data) => await http.put(`${API_NEWS_URL}/${data.id}`, data),
    onSuccess: (data) => {
      queryClient.invalidateQueries(['news.list'])
      message.success('更新成功')
    },
  })
}

export const useNewsDeleteMutation = () => {
  const { http } = useAxios()
  const message = useMessage()
  const queryClient = useQueryClient()
  return useMutation({
    mutationFn: async (ids) => await http.delete(`${API_NEWS_URL}`, { data: { ids } }),
    onSuccess: () => {
      queryClient.invalidateQueries(['news.list'])
      message.success('删除成功')
    },
  })
}

export const useNewsCreateMutation = (onSuccess) => {
  const { http } = useAxios()
  const message = useMessage()
  const queryClient = useQueryClient()
  return useMutation({
    mutationFn: async (data) => await http.post(API_NEWS_URL, data),
    onSuccess: () => {
      queryClient.invalidateQueries(['news.list'])
      message.success('保存成功').then(onSuccess)
    },
  })
}

// +--------------------------------------------
// + news category hooks
// + -- useNewsCategoryQuery 获取新闻分类列表
// + -- useNewsCategoryDetailQuery 获取新闻分类详情
// + -- useNewsCategoryUpdateQuery 更新新闻分类
// + -- useNewsCategoryCreateMutation 新建新闻分类
// + -- useNewsCategoryDeleteMutation 删除新闻分类
// +--------------------------------------------

export const useNewsCategoryQuery = () => {
  const { http } = useAxios()
  return useQuery({
    queryKey: ['news.category.list'],
    queryFn: async () => {
      return await http.get(`${API_NEWS_CATEGORY_URL}`)
    },
  })
}

export const useNewsCategoryDetailQuery = (id) => {
  const { http } = useAxios()
  return useQuery({
    queryKey: ['news.category.detail', id],
    queryFn: async () => {
      const response = await http.get(`${API_NEWS_CATEGORY_URL}/${id}`)
      return response.data
    },
    enabled: !!id,
  })
}

export const useNewsCategoryUpdateMutation = () => {
  const { http } = useAxios()
  const queryClient = useQueryClient()
  const message = useMessage()
  return useMutation({
    mutationFn: async (data) => await http.put(`${API_NEWS_CATEGORY_URL}/${data.id}`, data),
    onSuccess: () => {
      queryClient.invalidateQueries(['news.category.list'])
      message.success('更新成功')
    },
  })
}

export const useNewsCategoryCreateMutation = () => {
  const { http } = useAxios()
  const message = useMessage()
  const queryClient = useQueryClient()
  return useMutation({
    mutationFn: async (data) => await http.post(API_NEWS_CATEGORY_URL, data),
    onSuccess: () => {
      queryClient.invalidateQueries(['news.category.list'])
      message.success('保存成功')
    },
  })
}

export const useNewsCategoryDeleteMutation = () => {
  const { http } = useAxios()
  const message = useMessage()
  const queryClient = useQueryClient()
  return useMutation({
    mutationFn: async (ids) => await http.delete(`${API_NEWS_CATEGORY_URL}`, { data: { ids } }),
    onSuccess: () => {
      queryClient.invalidateQueries(['news.category.list'])
      message.success('删除成功')
    },
  })
}
